package com.dony.shop.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dony.common.utils.StringUtils;
import com.dony.shop.domain.GlobalModel;
import com.dony.shop.mapper.GlobalModelMapper;
import com.dony.shop.service.IGlobalModelService;
import com.dony.shop.util.ShopeeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 全球商品业务层处理
 *
 * @author sundw
 * @date 2022-03-08
 */
@Slf4j
@Service
public class GlobalModelServiceImpl extends ServiceImpl<GlobalModelMapper, GlobalModel> implements IGlobalModelService {


    @Autowired
    ShopeeUtil shopeeUtil;

    @Override
    public Map<Float, StringBuilder> getPriceMap(Long globalItemId) {
        //集合价格
        Map<Float, StringBuilder> priceMap = new HashMap<>();
        List<GlobalModel> skuList = this.list(new QueryWrapper<GlobalModel>().eq("global_item_id", globalItemId));
        skuList.forEach(sku -> {
            StringBuilder sb = priceMap.get(sku.getOriginalPrice());
            if (sb == null) {
                sb = new StringBuilder();
            }
            sb.append(sku.getGlobalModelSku()).append("、");
            priceMap.put(sku.getOriginalPrice(), sb);
        });
        return priceMap;
    }

    @Override
    public Boolean syncGlobalModel(Long merchantId, Long globalItemId) {
        this.remove(new QueryWrapper<GlobalModel>().eq("global_item_id", globalItemId));
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("global_item_id", globalItemId);
        JSONObject response = shopeeUtil.merchantGetHandler("/api/v2/global_product/get_global_model_list", merchantId, paramMap);
        if (response == null) {
            return null;
        }
        List<JSONObject> tiers = response.getList("tier_variation", JSONObject.class);
        List<JSONObject> globalModels = response.getList("global_model", JSONObject.class);
        //GlobalModel 可能为空
        if (globalModels == null) {
            return null;
        }
        List<GlobalModel> globalModelVos = globalModels.stream().map(model -> {
            GlobalModel modelVo = new GlobalModel();
            modelVo.setGlobalItemId(globalItemId);
            modelVo.setGlobalModelId(model.getLong("global_model_id"));
            modelVo.setMerchantId(merchantId);
            modelVo.setGlobalModelSku(model.getString("global_model_sku"));
            modelVo.setOriginalPrice(model.getJSONObject("price_info").getFloat("original_price"));
            modelVo.setCurrentStock(model.getList("stock_info", JSONObject.class).get(0).getInteger("current_stock"));
            //层级处理
            List<Integer> tierIndex = model.getList("tier_index", Integer.class);
            modelVo.setTierIndex(StringUtils.join(tierIndex, "-"));
            modelVo.setFirstIndex(tierIndex.get(0));
            if (tierIndex.size() > 1) {
                modelVo.setSecondIndex(tierIndex.get(1));
            }
            JSONObject image = tiers.get(0).getList("option_list", JSONObject.class).get(tierIndex.get(0)).getJSONObject("image");
            if (image != null) {
                modelVo.setImageUrl(image.getString("image_url"));
            } else {
                log.error("商品Id:{},没有sku图片", globalItemId);
            }
            return modelVo;
        }).collect(Collectors.toList());
        this.saveBatch(globalModelVos);
        return true;
    }

}
